/**
 * 确认表单
 */
layui.define(['yunj', "validate"], function (exports) {
    
    let validate = layui.validate;

    class YunjConfirmForm{

        constructor(args){
            this.args=args;
            this.formId = args.formId;          // 表单id
            this.fields = args.fields;          // 字段
            this.yes = args.yes;                // 确认回调

            this.init();
        }

        init(){
            let that=this;
            if (!that.fields || !yunj.isArray(that.fields) || that.fields.length <= 0) {
                return;
            }
            that.render();
        }

        // 渲染
        render(){
            let that = this;
            let options = {
                type: 0,
                title: "确认表单",
                content: "",
                area:"auto",
                btn: ["确认", "取消"],
                shadeClose: true,
                success: function(layero, index, layeroThat){
                    // 表单字段渲染
                    layero.find(".layui-layer-content").append(`<form class="layui-form layui-form-pane yunj-form action-confirm-form" lay-filter="${that.formId}"><div class="layui-row"></div></form>`);
                    that.handleFieldsRender(layero);
                },
                yes: function (index, layero, layeroThat) {
                    // 获取确认表单数据
                    let data = yunj.formData(that.formId,validate,layero);
                    that.yes && that.yes(data);
                    // 关闭弹出层
                    top.layer.close(index);
                }
            };

            yunj.isExistParent() ? parent.layer.open(options) : layer.open(options);
        }

        // 因为弹窗元素存于一个隔离的 DOM 环境中，直接通过全局的$是找不到的
        async handleFieldsRender(layero) {
            let that = this;
            for (let i=0;i<that.fields.length;i++) {
                let fieldArgs = that.fields[i]
                await new Promise(resolve => {
                    yunj.formField(fieldArgs.type, {
                        outermostEl:layero,
                        formId: that.formId,
                        args: fieldArgs
                    }).then(field => {
                        return field.render(`.yunj-form[lay-filter=${that.formId}] > .layui-row`);
                    }).catch(err => {
                        console.error(err);
                        resolve();
                    });
                });
            }
            // 处理字段样式
            if (!yunj.isMobile()) {
                // label 宽度
                let labelMaxWidth = 0;
                let currFormItemEl = layero.find('.yunj-form-item:not(.yunj-form-item-label-width-fill,:has(.yunj-form-item-label-width-fill))');
                currFormItemEl.find('.layui-form-label').css('width', 'auto');
                currFormItemEl.each(function () {
                    let currLabelWidth = $(this).find('.layui-form-label').outerWidth();
                    if (currLabelWidth > labelMaxWidth) labelMaxWidth = currLabelWidth;
                });
                let inc = 1;
                let labelWidth = labelMaxWidth + inc;
                currFormItemEl.find('.layui-form-label').css('width', labelWidth + 'px');
            }
        }

    }

    let confirmForm = (args={})=>{
        args=yunj.objSupp(args,{
            formId:'',
            fields:[],
            yes:null,
        });
        return new YunjConfirmForm(args);
    };

    exports('confirmForm', confirmForm);
});